package Cal;

public class CalculatorStack {
    private DataNode head = null;

    /**
     * 获取头部节点的信息

     */
    public DataNode getHead() {
        return head;
    }

    /**
     构造器，初始化头部节点

     */
    public CalculatorStack() {
        head = new DataNode("");
    }

    /**
     * 判断栈是否为空

     */
    public boolean isEmpty() {
        return head.getNext() == null;
    }

    /**
     * 压栈操作

     */
    public void push(String value) {
        DataNode DataNode = new DataNode(value);
        DataNode.setNext(head.getNext());
        head.setNext(DataNode);
    }

    /**
     * 弹栈操作

     */
    public String pop() {
        if (isEmpty()) {
            throw new RuntimeException("表达式有误！");
        }
        String value = head.getNext().getData();
        head = head.getNext();
        return value;
    }

    /**
     * 获取栈顶节点信息

     */
    public String peek() {
        if (isEmpty())
            throw new RuntimeException("表达式有误！");
        return head.getNext().getData();
    }

    /**
     * 显示栈的情况[遍历栈]，遍历时，需要从栈顶开始显示数据

     */
    public void list() {
        if (isEmpty()) {
            System.out.println("栈空！");
            return;
        }
        DataNode h = head;
        while (h.getNext() != null) {
            h = h.getNext();
            System.out.println(h);
        }
    }
}
